target := riscv64imac-unknown-none-elf
mode ?= debug
kernel := target/$(target)/$(mode)/riscv
bin := target/$(target)/$(mode)/kernel.bin

objdump := rust-objdump --arch-name=riscv64
objcopy := rust-objcopy --binary-architecture=riscv64

ifeq ($(mode), release)
	BUILD_ARGS += --release
endif

.PHONY: build clean qemu run env

build: $(bin)

env:
	cargo install cargo-binutils
	rustup component add llvm-tools-preview rustfmt
	rustup target add $(target)

$(kernel):
	cargo build $(BUILD_ARGS)

$(bin): $(kernel)
	$(objcopy) $(kernel) --strip-all -O binary $@

asm:
	$(objdump) -d $(kernel) | less

sym:
	$(objdump) -t $(kernel) | less

clean:
	cargo clean

qemu: $(bin)
	qemu-system-riscv64 \
		-machine virt \
		-nographic \
		-bios default \
		-device loader,file=$(bin),addr=0x80200000

run: build qemu
